package kr.couponplay.android.activity.deal.list;

import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.deal.DealListActivity;
import kr.couponplay.android.domain.deal.DealListResult;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;

public class DealListViewPagerAdapter extends FragmentStatePagerAdapter {
	private DealListFragmentFactory fragmentFactory;
	private DealListActivity activity;
	
	public DealListViewPagerAdapter(DealListActivity activity, DealListResult dealListBox) {
		super(activity.getSupportFragmentManager());
        this.activity = activity;
		this.fragmentFactory = new DealListFragmentFactory(activity, dealListBox);
    }
	
	public void replace() {
		for (int i = 0; i < getCount(); i++) {
			FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.sub_menu_viewpager, fragmentFactory.get(i));
			transaction.commit();
		}
        notifyDataSetChanged();
    }	

	@Override
	public Fragment getItem(int position) {
		return fragmentFactory.get(position);
    }
	
	@Override
	public int getItemPosition(Object item) {
		DealListFragment fragment = (DealListFragment)item;
        return fragment.getPosition();
    }
	
	@Override
	public int getCount() {
		return fragmentFactory.getCount();
	}
}
